# _*_ encoding:utf-8 _*_

from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import IsAdminUser
from rest_framework.generics import ListAPIView

from goods.models import SPU, Brand
from meiduo_admin.paginations import MyPage
from meiduo_admin.serializers.spu_serializers import SPUGoodsSerialzier, BrandSimpleModelSerialzier


class BrandeView(ListAPIView):
    permission_classes = [IsAdminUser]

    queryset = Brand.objects.all()
    serializer_class = BrandSimpleModelSerialzier


class SPUGoodsView(ModelViewSet):

    permission_classes = [IsAdminUser]

    queryset = SPU.objects.all()
    serializer_class = SPUGoodsSerialzier
    pagination_class = MyPage

    def get_queryset(self):

        keyword = self.request.query_params.get('keyword')

        if keyword:
            return self.queryset.filter(name__contains=keyword)
        else:
            return self.queryset.all()

